package com.hlj.ncp.business.util;

import com.hlj.ncp.business.entity.Customer;
import com.hlj.ncp.business.entity.Merchant;
import com.hlj.ncp.business.entity.Supplier;
import com.hlj.ncp.business.service.ICustomerService;
import com.hlj.ncp.business.service.IMerchantService;
import com.hlj.ncp.business.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 数据初始化工具类
 * 在应用启动时自动执行，初始化必要的业务数据
 */
@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private IMerchantService merchantService;
    
    @Autowired
    private ICustomerService customerService;
    
    @Autowired
    private ISupplierService supplierService;

    @Override
    public void run(String... args) throws Exception {
        // 检查是否已有数据，如果没有则初始化
        if (merchantService.count() == 0) {
            initMerchantData();
        }
        
        if (customerService.count() == 0) {
            initCustomerData();
        }
        
        if (supplierService.count() == 0) {
            initSupplierData();
        }
    }
    
    private void initMerchantData() {
        Merchant merchant1 = new Merchant();
        merchant1.setMerchantId(101L);
        merchant1.setMerchantName("哈尔滨农产品贸易有限公司");
        merchant1.setMerchantType("0");
        merchant1.setContactName("张经理");
        merchant1.setContactPhone("13800138001");
        merchant1.setContactEmail("zhangm@example.com");
        merchant1.setAddress("哈尔滨市道里区中央大街123号");
        merchant1.setBusinessScope("农产品批发、零售");
        merchant1.setStatus("0");
        merchant1.setCreateBy("admin");
        merchant1.setCreateTime(LocalDateTime.now());
        merchant1.setRemark("重要商户");
        
        Merchant merchant2 = new Merchant();
        merchant2.setMerchantId(102L);
        merchant2.setMerchantName("黑龙江绿色食品有限公司");
        merchant2.setMerchantType("1");
        merchant2.setContactName("李总");
        merchant2.setContactPhone("13900139002");
        merchant2.setContactEmail("liz@example.com");
        merchant2.setAddress("哈尔滨市南岗区文化大街456号");
        merchant2.setBusinessScope("有机食品加工、销售");
        merchant2.setStatus("0");
        merchant2.setCreateBy("admin");
        merchant2.setCreateTime(LocalDateTime.now());
        merchant2.setRemark("长期合作伙伴");
        
        Merchant merchant3 = new Merchant();
        merchant3.setMerchantId(103L);
        merchant3.setMerchantName("双城区农产品合作社");
        merchant3.setMerchantType("0");
        merchant3.setContactName("王主任");
        merchant3.setContactPhone("13700137003");
        merchant3.setContactEmail("wangzr@example.com");
        merchant3.setAddress("哈尔滨市双城区新发镇农业园区");
        merchant3.setBusinessScope("农产品种植、销售");
        merchant3.setStatus("0");
        merchant3.setCreateBy("admin");
        merchant3.setCreateTime(LocalDateTime.now());
        
        merchantService.saveBatch(Arrays.asList(merchant1, merchant2, merchant3));
    }
    
    private void initCustomerData() {
        Customer customer1 = new Customer();
        customer1.setCustomerId(101L);
        customer1.setCustomerName("北京连锁超市集团");
        customer1.setCustomerType("0");
        customer1.setContactName("刘经理");
        customer1.setContactPhone("13600136001");
        customer1.setContactEmail("lium@example.com");
        customer1.setAddress("北京市朝阳区建国路789号");
        customer1.setBusinessNeeds("需要大量新鲜蔬菜水果，每周配送");
        customer1.setVipFlag("1");
        customer1.setStatus("0");
        customer1.setCreateBy("admin");
        customer1.setCreateTime(LocalDateTime.now());
        customer1.setRemark("VIP客户");
        
        Customer customer2 = new Customer();
        customer2.setCustomerId(102L);
        customer2.setCustomerName("天猫生鲜旗舰店");
        customer2.setCustomerType("1");
        customer2.setContactName("赵店长");
        customer2.setContactPhone("13500135002");
        customer2.setContactEmail("zhaodz@example.com");
        customer2.setAddress("杭州市余杭区阿里巴巴园区");
        customer2.setBusinessNeeds("需要高品质农产品，冷链配送");
        customer2.setVipFlag("1");
        customer2.setStatus("0");
        customer2.setCreateBy("admin");
        customer2.setCreateTime(LocalDateTime.now());
        customer2.setRemark("电商平台合作伙伴");
        
        Customer customer3 = new Customer();
        customer3.setCustomerId(103L);
        customer3.setCustomerName("哈尔滨饭店集团");
        customer3.setCustomerType("0");
        customer3.setContactName("孙总厨");
        customer3.setContactPhone("13400134003");
        customer3.setContactEmail("sunzc@example.com");
        customer3.setAddress("哈尔滨市道外区东直路321号");
        customer3.setBusinessNeeds("需要新鲜食材，每日配送");
        customer3.setVipFlag("0");
        customer3.setStatus("0");
        customer3.setCreateBy("admin");
        customer3.setCreateTime(LocalDateTime.now());
        customer3.setRemark("本地客户");
        
        customerService.saveBatch(Arrays.asList(customer1, customer2, customer3));
    }
    
    private void initSupplierData() {
        Supplier supplier1 = new Supplier();
        supplier1.setSupplierId(101L);
        supplier1.setSupplierName("黑龙江快运物流公司");
        supplier1.setSupplierType("0");
        supplier1.setContactName("钱经理");
        supplier1.setContactPhone("13300133001");
        supplier1.setContactEmail("qianm@example.com");
        supplier1.setAddress("哈尔滨市香坊区和平路111号");
        supplier1.setServiceItems("冷链运输、普通货运");
        supplier1.setServicePrice("冷链：2元/公里/吨，普通：1元/公里/吨");
        supplier1.setPerformance("服务及时，车辆设备完善");
        supplier1.setStatus("0");
        supplier1.setCreateBy("admin");
        supplier1.setCreateTime(LocalDateTime.now());
        supplier1.setRemark("长期合作物流公司");
        
        Supplier supplier2 = new Supplier();
        supplier2.setSupplierId(102L);
        supplier2.setSupplierName("顺丰速运");
        supplier2.setSupplierType("1");
        supplier2.setContactName("周主管");
        supplier2.setContactPhone("13200132002");
        supplier2.setContactEmail("zhouzg@example.com");
        supplier2.setAddress("哈尔滨市南岗区哈西大街222号");
        supplier2.setServiceItems("快递服务、冷链配送");
        supplier2.setServicePrice("详见合同附件");
        supplier2.setPerformance("服务质量高，速度快");
        supplier2.setStatus("0");
        supplier2.setCreateBy("admin");
        supplier2.setCreateTime(LocalDateTime.now());
        supplier2.setRemark("全国性快递公司");
        
        Supplier supplier3 = new Supplier();
        supplier3.setSupplierId(103L);
        supplier3.setSupplierName("双城区运输车队");
        supplier3.setSupplierType("0");
        supplier3.setContactName("吴队长");
        supplier3.setContactPhone("13100131003");
        supplier3.setContactEmail("wudz@example.com");
        supplier3.setAddress("哈尔滨市双城区交通路333号");
        supplier3.setServiceItems("农产品运输、装卸服务");
        supplier3.setServicePrice("0.8元/公里/吨");
        supplier3.setPerformance("本地运力充足，价格优惠");
        supplier3.setStatus("0");
        supplier3.setCreateBy("admin");
        supplier3.setCreateTime(LocalDateTime.now());
        supplier3.setRemark("本地车队");
        
        supplierService.saveBatch(Arrays.asList(supplier1, supplier2, supplier3));
    }
} 